<!DOCTYPE html>
<html lang="en">

<head>
    <title>vuex</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <link href="css/style.css" rel="stylesheet"> -->
</head>

<body>
    <div id="app">
        <div> {{count}}</div>
        <my-addbtn :count="count"></my-addbtn>
        <my-removebtn :count="count"></my-removebtn>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>

    <script>
        var busVm = new Vue(); //空实例

        var vm = new Vue({
            el: "#app",
            data: {
                count: 0
            },
            mounted:function(){
                busVm.$on("changeEvent",function(num){
                    this.count=num;
                }.bind(this));
            },
            components: {
                "my-addbtn": {
                    template: '<button @click="setCount">+</button>',
                    props:["count"],
                    methods:{
                        setCount:function(){
                            busVm.$emit("changeEvent",this.count-1);
                        }
                    }
                },
                "my-removebtn": {
                    template: '<button @click="setCount">-</button>',
                    props:["count"],
                    methods:{
                        setCount:function(){
                            busVm.$emit("changeEvent",this.count-1);
                        }
                    }
                }
            }
        });
    </script>
</body>

</html>